﻿using Microsoft.Office.Interop.Excel;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class SaveOrSaveAsExcelNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            SaveOrSaveAsExcelNParam? param = item.ControlParam as SaveOrSaveAsExcelNParam;
            string saveObject = param.SaveObject;
            Workbook wb = this.GetObjVar<ExcelModel>(ref saveObject, globleData).WorkBookValue as Workbook;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", saveObject));
            }
            int saveWay = param.SaveWay;
            string savePath = param.SaveAsPath;
            this.GetStringVar(ref savePath, globleData);
            bool cover = param.Cover;
            bool close = param.Close;
            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);
            if(saveWay == 0) {
                //保存
                excelUtil.SetApp(wb);
                excelUtil.SaveWorkBook();
            } else { 
                //另存为
                excelUtil.WorkBookSaveAs(savePath, cover);
            }
            if(close) { 
                excelUtil.CloseCurrentExcel();
            }
            return currentIndex;
        }
        
    }
}
